<template>
  <div>
    <el-table :data="store.getters['role/getRole']" border style="width: 100%">
      <el-table-column prop="id" label="角色编号" width="180" />
      <el-table-column prop="rolename" label="角色名称" width="180" />
      <el-table-column prop="status" label="状态">
        <template v-slot="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="item">
          <div>
            <el-button type="primary" size="small" @click="edit(item.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="small" @click="del(item.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { defineEmits } from "vue";
import { useStore } from "vuex";
import { roledelete } from "../../request/api";
import { ElMessageBox, ElMessage } from "element-plus";
let store = useStore();

let emit = defineEmits<{
  (e: "edit", value: number): void;
}>();
// 封装一个编辑事件
let edit = (id: number) => {
  emit("edit", id);
};
// 删除事件
let del = (id: number) => {
  ElMessageBox.confirm("你确定要删除吗", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      roledelete({ id }).then((res) => {
        if (res.code == 200) {
          ElMessage.success(res.msg);
          getRole()
        } else {
          ElMessage.error(res.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除取消",
      });
    });
};
// 调用role仓库
const getRole = () => {
  store.dispatch("role/getRoleActions");
};
getRole();
</script>

<style lang="less" scoped></style>
